package com.witframework.core.logic;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.witframework.WitException;
import com.witframework.WitServiceException;
import com.witframework.core.persistence.IBaseDao;
import com.witframework.core.persistence.PageSortModel;
import com.witframework.core.persistence.SimpleHibernateDao;

public class BaseService implements IBaseService{	
	
	public IBaseDao dao;
	public void init(Class entityClass){		
		init(entityClass,false);		
	}
	public void init(Class entityClass,boolean isMem){		
		SimpleHibernateDao simpleHibernateDao=new SimpleHibernateDao();//(SimpleHibernateDao)BeanFactory.getBean("simpleHibernateDao");
		simpleHibernateDao.setEntityClass(entityClass,isMem);		
		dao=simpleHibernateDao;	
	}
	
	public void setDao(IBaseDao dao) {
		this.dao = dao;
	}
	public IBaseDao getDao(){
		return dao;
	}	
	public Object get(String id) throws WitServiceException {
		return getDao().findById(id);
	}

	public Object get(java.io.Serializable id) throws WitServiceException {
		return getDao().findById(id);
	}
	public int getAllSize()throws WitServiceException {
		return getDao().getAllSize();
	}
	public List getAll() throws WitServiceException {
			return getDao().getAll();
	}
	
	public String save(Object entity) throws WitServiceException {
		return getDao().save(entity);
	}
	public void saveAll(Collection entityCollection) throws WitServiceException{
		getDao().saveAll(entityCollection);
	}
	public void update(Object entity) throws WitServiceException {
		getDao().update(entity);
	}
	
	public String saveOrUpdate(Object entity) throws WitServiceException {		
		return getDao().saveOrUpdate(entity);		
	}
	
	public int saveOrUpdateAll(List entityList) throws WitServiceException {
		getDao().saveOrUpdateAll(entityList);
		return entityList.size();
	}
	
	public void deleteById(final String id) throws WitServiceException {
		getDao().deleteById(id);
	}
	
	public void delete(Object entity) throws WitServiceException {
		getDao().delete(entity);
	}
	
	public void deleteBy(String propertyName, Object value)	throws WitServiceException {
		getDao().deleteBy(propertyName, value);
	}
	
	public void deleteAll(Collection  entities) throws WitServiceException {
		getDao().deleteAll(entities);
	}
	
	public void deleteAllByIds(String... ids) throws WitServiceException {
		getDao().deleteAllByIds(ids);
	}


	public List findBy(Map filter)	throws WitServiceException {
		return getDao().findBy( filter);
	}
	public List findBy(Map filter,String orderby ,boolean isAsc)	throws WitServiceException {
		return getDao().findBy(filter,orderby,isAsc);
	}

	public List findBy(String propertyName, Object value)throws WitServiceException {
		return getDao().findBy(propertyName, value);
	}
	
	public Object getBy(String propertyName, Object value)throws WitServiceException {
		return getDao().getBy(propertyName, value);
	}

	public List pagedQuery(Map filter, PageSortModel psm)throws WitServiceException {
		return getDao().pagedQuery(filter,psm);
	}
	
	public Object merge(Object entity) throws WitServiceException {
		return getDao().merge(entity);
	}

	public List findByNamedQuery(String queryName) throws WitServiceException {
		return getDao().findByNamedQuery(queryName);
	}

	public List findByNamedQuery(String queryName, Object value)throws WitServiceException {
		return getDao().findBy(queryName, value);
	}

	public List findByNamedQuery(String queryName, Object[] values)	throws WitServiceException {
		return getDao().findByNamedQuery(queryName, values);
	}

	public List findByNamedQueryAndNamedParam(String queryName,	String paramName, Object value) {
		return getDao().findByNamedQueryAndNamedParam(queryName, paramName, value);
	}

	public List findByNamedQueryAndNamedParam(String queryName,
			String[] paramNames, Object[] values) throws WitServiceException {
		return getDao().findByNamedQueryAndNamedParam(queryName, paramNames, values);
	}

	public List pagedQueryByNamedQuery(String queryName, PageSortModel psm)	throws WitServiceException {
		return getDao().pagedQueryByNamedQuery(queryName, psm);
	}

	public List pagedQueryByNamedQuery(String queryName, Object value,	PageSortModel psm) throws WitServiceException {
		return getDao().pagedQueryByNamedQuery(queryName, value, psm);
	}

	public List pagedQueryByNamedQuery(String queryName, Object[] values,	PageSortModel psm) throws WitServiceException {
		return getDao().pagedQueryByNamedQuery(queryName, values, psm);
	}

	public List pagedQueryByNamedQueryAndNamedParam(String queryName,String paramName, Object value, PageSortModel psm) {
		return getDao().pagedQueryByNamedQueryAndNamedParam(queryName, paramName, value, psm);
	}

	public List pagedQueryByNamedQueryAndNamedParam(String queryName,String[] paramNames, Object[] values, PageSortModel psm) {
		return getDao().pagedQueryByNamedQueryAndNamedParam(queryName, paramNames, values, psm);
	}
	public Object findObjectBy(Map filter) throws WitServiceException {
		List l=this.findBy(filter);
		if(l.size()==1) return l.get(0);
		else if(l.size()>1) throw new WitException("期望的返回结果数是1,实际是："+l.size());
		else return null;
	}

}